package com.yd.app.mapper.provider;

import com.yd.domain.searchParam.OldParam;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-03-08 10:27
 * @desc 旧物管理
 **/
public class OldsRetrieveProvider  extends BaseProvider {
    /**
     * @Author: zy
     * @Desc: 数据查询列表
     * @Date: 2018/3/8
     * @Param
     */
    public String searchOldList(Map map) {
        OldParam oldParam = (OldParam) map.get("oldParam");
        StringBuilder sb = new StringBuilder("select o.id,d_sta.title as statusName,d_del.title AS dealStatusName,dic.typeName as typeName ,cu.realName as checkUserName," +
                "addu.realName as addUserName,o.addUser,o.`status`,o.checkTime,o.checkUser,o.createTime,o.dealStatus,o.des,o.ifPic,o.perWeight,o.price,o.title,o.type,o.dealTime,o.remark,o.iconUrl  " +
                " FROM business_old o " +
                " LEFT JOIN business_gType dic ON dic.id=o.type " +
                " LEFT JOIN sys_dic d_sta ON d_sta.`code`=o.`status` and d_sta.type='oldStatus'" +
                " LEFT JOIN sys_dic d_del ON d_del.`code`=o.dealStatus and d_del.type='oldDelSta'" +
                " LEFT JOIN sys_user addu ON o.addUser=addu.id" +
                " LEFT JOIN sys_user cu ON o.checkUser=cu.id" +
                " WHERE 1=1 ");
        sb.append(createSearchSanWhereSql(oldParam));
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据条数
     * @Date: 2018/3/7
     */
    public String searchOldCount(Map map) {
        OldParam oldParam = (OldParam) map.get("oldParam");
        StringBuilder sb = new StringBuilder("SELECT COUNT(1) from business_old o  " +
                "  where 1=1 ");
        sb.append(createSearchSanWhereSql(oldParam));
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:权限查询条件
     * @Date: 2018/3/7
     */
    private String createSearchSanWhereSql(OldParam oldParam) {
        StringBuilder sb = new StringBuilder();
        if (!StringUtils.isEmpty(oldParam.getTitle())) {
            sb.append(" and o.title like  '%" + oldParam.getTitle() + "%'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getType())) {
            sb.append(" and o.type = '" + oldParam.getType() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getCheckSTime())) {
            sb.append(" and o.checkTime >=#{oldParam.getCheckSTime}  ");
        }
        if (!StringUtils.isEmpty(oldParam.getCheckETime())) {
            sb.append(" and o.checkTime <=#{oldParam.getCheckETime}  ");
        }
        if (!StringUtils.isEmpty(oldParam.getAddUser())) {
            sb.append(" and o.addUser = '" + oldParam.getAddUser() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getCheckUser())) {
            sb.append(" and o.checkUser = '" + oldParam.getCheckUser() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getDealStatus())) {
            sb.append(" and o.dealStatus = '" + oldParam.getDealStatus() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getIfPic())) {
            sb.append(" and o.ifPic = '" + oldParam.getIfPic() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getStatus())) {
            sb.append(" and o.`status` = '" + oldParam.getStatus() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getType())) {
            sb.append(" and o.type = '" + oldParam.getType() + "'  ");
        }
        if (!StringUtils.isEmpty(oldParam.getCreSTime())) {
            sb.append(" and o.createTime >=#{oldParam.getCreSTime}  ");
        }
        if (!StringUtils.isEmpty(oldParam.getCreETime())) {
            sb.append(" and o.createTime <=#{oldParam.getCreETime}  ");
        }
        return sb.toString();
    }
    /**
     * @Author: zy
     * @param map
     */
    public String searchMyOldList(Map map) {
        OldParam oldParam = (OldParam) map.get("oldParam");
        StringBuilder sb = new StringBuilder("select * from business_old o " +
                " WHERE 1=1 ");
        sb.append(createSearchSql(oldParam));
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }

    /**
     * @Author: zy
     * @param map
     */
    public String searchMyOldCount(Map map) {
        OldParam oldParam = (OldParam) map.get("oldParam");
        StringBuilder sb = new StringBuilder("SELECT COUNT(1) from business_old o  " +
                "  where 1=1 ");
        sb.append(createSearchSql(oldParam));
        return sb.toString();
    }

    /**
     * @Author: zy
     * @param oldParam
     * @return
     */
    private String createSearchSql(OldParam oldParam) {
        StringBuilder sb = new StringBuilder();
        if (!StringUtils.isEmpty(oldParam.getAddUser())) {
            if (!StringUtils.isEmpty(oldParam.getAddUser())) {
                sb.append(" and o.addUser = '" + oldParam.getAddUser() + "'  ");
            }
            if (!StringUtils.isEmpty(oldParam.getCreSTime())) {
                sb.append(" and o.createTime >=#{oldParam.getCreSTime}  ");
            }
            if (!StringUtils.isEmpty(oldParam.getCreETime())) {
                sb.append(" and o.createTime <=#{oldParam.getCreETime}  ");
            }

        }
        return sb.toString();
    }
}
